import {ref} from "vue"
import {HttpResult} from "@/types/project";
import store from "@/store/index"
import configureStore from "@/apps/admin/store/configure"
import {getConfigureApi} from "@/apps/admin/api/common"
import {Ref, UnwrapRef} from "@vue/reactivity";

/**
 * 获取配置
 * @param auto
 */
export function useConfigure(auto: boolean = false): { conf: any, getAppConfigure: () => void } {
    //定时任务日志
    const task: Ref<UnwrapRef<null | number>> = ref(null)

    //全局配置状态
    const config = configureStore(store)

    /**
     * 获取系统配置
     */
    function getAppConfigure() {
        if (task.value !== null) clearTimeout(task.value)
        getConfigureApi().then((result: HttpResult) => {
            if (result.code === 200) {
                config.setConfigure(result.data)
            }
            task.value = setTimeout(getAppConfigure, 60000)
        })
    }

    return {
        getAppConfigure,
        conf: config.conf
    }
}